package com.qst.service.ssm;

import com.qst.ExamException;
import com.qst.dao.*;
import com.qst.entity.Schedule;
import com.qst.entity.User;
import com.qst.mapper.AssessmentMapper;
import com.qst.mapper.ScheduleMapper;
import com.qst.mapper.TeamMapper;
import com.qst.mapper.UserMapper;
import com.qst.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;


@Service
@SuppressWarnings("all")
public class ScheduleServiceSsmImpl implements ScheduleService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    ScheduleMapper scheduleMapper;
    @Autowired
    AssessmentMapper assessmentTypeMapper;
    @Autowired
    private TeamMapper teamMapper;

    @Override
    public List<Schedule> findByCreator(User user) {
        List<Schedule> scheduleList = scheduleMapper.findByCreatorId(user.getId());
        for (Schedule h : scheduleList) {
            h.setCreator(user);
            h.setAssessmentType(assessmentTypeMapper.findById(h.getAssessmentId()));
            h.setTeam(teamMapper.findById(h.getTeamId()));
        }
        return scheduleList;
    }

    @Override
    public Schedule findById(Integer id) {
        Schedule h = scheduleMapper.findById(id);
        h.setCreator(userMapper.findById(h.getCreatorId()));
        h.setAssessmentType(assessmentTypeMapper.findById(h.getAssessmentId()));
        h.setTeam(teamMapper.findById(h.getTeamId()));
        return h;
    }

    @Override
    public void saveSchedule(Schedule h) {
        scheduleMapper.save(h);
    }

    @Override
    public Schedule deleteSchedule(Integer id) {
        Schedule sche = scheduleMapper.findById(id);
        if (sche.getStatus() > 1) {
            throw new ExamException("只有未开始或作废的安排才能删除");
        }
        scheduleMapper.delete(sche.getId());
        return sche;
    }

    @Override
    public void updateSchedule(Schedule h) {
        scheduleMapper.update(h);
    }
}
